package ducna.android.aoeclip.screen.category.loader;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import ducna.android.aoeclip.common.model.business.JSONParser;
import ducna.android.aoeclip.common.model.entity.Category;
import ducna.android.aoeclip.common.utils.Constant;
import ducna.android.aoeclip.common.utils.Logger;

/**
 * Created by DUCNA on 11/4/2014.
 */
public class DataCategoriesLoader extends AsyncTaskLoader<ArrayList<Category>> implements Constant {
    private String TAG = DataCategoriesLoader.class.getSimpleName();
    private ArrayList<Category> mResult;

    /**
     * @param context
     */
    public DataCategoriesLoader(Context context) {
        super(context);
    }

    @Override
    public ArrayList<Category> loadInBackground() {
        ArrayList<Category> dataCategory = new ArrayList<Category>();

        JSONObject categoriesJson = JSONParser.getJSONFromUrl(URL_CATEGORY_LIST);
        Logger.out(TAG, "category json : " + categoriesJson.toString());

        if (categoriesJson != null) {
            try {
                JSONArray arrServices = categoriesJson.getJSONArray("store8x");
                for (int i = 0; i < arrServices.length(); i++) {
                    JSONObject objCategory = arrServices.getJSONObject(i);
                    String id = objCategory.getString(KEY_CATEID);
                    String catename = objCategory.getString(KEY_CATE_NAME);
                    String cateimages = objCategory.getString(KEY_CATE_ICON);
                    dataCategory.add(new Category(id, catename, cateimages));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return dataCategory;
    }

    @Override
    protected void onStartLoading() {
        if (mResult != null)
            deliverResult(mResult);
        if (takeContentChanged() || mResult == null)
            forceLoad();
    }

    @Override
    protected void onStopLoading() {
        cancelLoad();
    }

    @Override
    protected void onReset() {
        super.onReset();
        // Ensure the loader is stopped
        onStopLoading();
        // At this point we can release the resources associated with 'apps'
        // if needed.
        if (mResult != null) {
            onReleaseResources(mResult);
            mResult = null;
        }
        // Stop monitoring for changes.
    }

    @Override
    public void deliverResult(ArrayList<Category> data) {
        if (isReset()) {
            // An async query came in while the loader is stopped. We
            // don't need the result.
            if (data != null)
                onReleaseResources(data);
        }
        ArrayList<Category> oldData = data;
        mResult = data;
        if (isStarted()) {
            // If the Loader is currently started, we can immediately
            // deliver its results.
            super.deliverResult(data);
        }
        // At this point we can release the resources associated with
        // 'oldApps' if needed; now that the new result is delivered we
        // know that it is no longer in use.
        if (oldData != null) {
            onReleaseResources(oldData);
        }
    }

    @Override
    public void onCanceled(ArrayList<Category> data) {
        super.onCanceled(data);
        onReleaseResources(data);
    }

    protected void onReleaseResources(ArrayList<Category> data) {
        // For a simple List<> there is nothing to do. For something
        // like a Cursor, we would close it here.
    }
}
